package com.bestcem.xm.user.util.business.captcha;

import lombok.Getter;
import lombok.Setter;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.UnsupportedEncodingException;
import java.util.Random;

/**
 * 图片验证码对象
 *
 * @author wangyunyun <yunyun.wang@idiaoyan.com>
 * @version v1.0
 * @date 2022/6/8 11:05
 */
@Getter
@Setter
public abstract class Captcha {

    /**
     * 字符数量
     */
    private Integer size;

    /**
     * 干扰线数量
     */
    private Integer lines;

    /**
     * 宽度
     */
    private Integer width;

    /**
     * 高度
     */
    private Integer height;

    /**
     * 字体大小
     */
    private Integer fontSize;

    /**
     * 字体
     */
    private Font font;

    /**
     * 字体倾斜
     */
    private Boolean tilt;

    /**
     * 字体倾斜的角度
     */
    private Integer thetaRange;

    /**
     * 背景颜色
     */
    private Color backgroundColor;

    /**
     * 验证码
     */
    private String vscode;

    /**
     * 验证码缓存key
     */
    private String key;

    /**
     * 验证码图片对象
     */
    private BufferedImage image;

    /**
     * Captcha无参构造器,用于设置缺省值
     *
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/6/8 13:13
     */
    protected Captcha() {
        this.size = 4;
        this.lines = 6;
        this.width = 100;
        this.height = 40;
        this.fontSize = 24;
        this.tilt = true;
        this.thetaRange = 30;
        this.backgroundColor = Color.white;
        this.font = new Font("Microsoft YaHei UI", Font.BOLD, this.getFontSize());
    }

    /**
     * 生成随机码
     *
     * @param ran 随机
     * @return String
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/6/8 11:32
     */
    public abstract String randomCode(Random ran) throws UnsupportedEncodingException;
}
